<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\controller\shiyao;

use app\common\service\store\User as UserService;
use app\store\controller\Controller;
use app\store\model\shiyao\Project as ProjectModel;
use think\facade\Db;

/**
 * 项目控制器
 * Class StoreUser
 * @package app\store\controller
 */
class Project extends Controller {

	public function listAll() {
		$model = new ProjectModel;
		$list = $model->getListAll($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	public function listCompany() {
		$model = new ProjectModel;
		$list = $model->getListCompany($this->request->param());
		$store_user = UserService::getLoginInfo();
		$store_user = $store_user['user'];

		//$type = Db::name('shiyao_company')->where('company_id', $store_user['company_id'])->value('type');

		return $this->renderSuccess(compact('list'));
	}

	public function listCenter() {
		$model = new ProjectModel;
		$list = $model->getListCenter($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	public function state(array $goodsIds, bool $state) {
		$model = new ProjectModel;
		if (!$model->setStatus($goodsIds, $state)) {
			return $this->renderError($model->getError() ?: '操作失败');
		}
		return $this->renderSuccess('操作成功');
	}

	/**
	 * 管理员列表
	 * @return array
	 * @throws \think\db\exception\DbException
	 */
	public function list() {
		$model = new ProjectModel;
		$list = $model->getList($this->request->param());
		return $this->renderSuccess(compact('list'));
	}
	public function detail(int $projectId) {

		$detail = ProjectModel::detail($projectId);
		return $this->renderSuccess(compact('detail'));
	}
	/**
	 *
	 * @return array
	 */
	public function add() {
		// 新增记录
		$model = new ProjectModel;
		if ($model->add($this->postForm())) {
			return $this->renderSuccess('添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	/**
	 *添加PM
	 * @return array
	 */
	public function addPm(int $projectId) {
		// 管理员详情
		$model = ProjectModel::detail($projectId);
		if ($model->addPm($this->postForm())) {
			return $this->renderSuccess('添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	/**
	 *
	 * @param int $projectId
	 * @return array
	 */
	public function edit(int $projectId) {
		// 管理员详情
		$model = ProjectModel::detail($projectId);
		// 更新记录
		if ($model->edit($this->postForm())) {
			return $this->renderSuccess('更新成功');
		}
		return $this->renderError($model->getError() ?: '更新失败');
	}

	public function audit(int $projectId) {
		// 管理员详情
		$model = ProjectModel::detail($projectId);
		// 更新记录
		if ($model->audit($this->postForm())) {
			return $this->renderSuccess('审核成功');
		}
		return $this->renderError($model->getError() ?: '审核失败');
	}

	/**
	 * 删除管理员
	 * @param int $projectId
	 * @return array|bool
	 */
	public function delete(int $projectId) {
		// 管理员详情
		$model = ProjectModel::detail($projectId);
		if (!$model->setDelete()) {
			return $this->renderError($model->getError() ?: '删除失败');
		}
		return $this->renderSuccess('删除成功');
	}

}
